<?php
class Admin_Model_UserGroup extends Zend_Db_Table {
	protected $_name    = 'user_group';
	protected $_primary = 'id';

	// count number item user group
	public function countItem($arrParam = null, $options = null) {
		$db = Zend_Registry::get('connectDb');
		$select = $db->select()
		 			 ->from('user_group AS g', array('COUNT(g.id) AS totalItem'));
		$result = $db->fetchOne($select);
		return $result;
	}
	
	// view list item
	public function listItem($arrParam = null, $options = null) {
		$db = Zend_Registry::get('connectDb');
		$paginator = $arrParam['paginator'];
		$ssFilter = $arrParam['ssFilter'];
	    if($options['task'] == 'admin-list') {
	    	$select = $db->select()
	    	             ->from('user_group AS g', array('id','group_name','status','group_acp','order'))
	    	             ->joinLeft('users AS u', 'g.id = u.group_id','COUNT(u.id) AS members')
	    	             ->group('g.id');
	    	
	    	if(!empty($ssFilter['col']) && !empty($ssFilter['order'])) {
	    		$select->order($ssFilter['col'] . ' ' . $ssFilter['order']);
	    	}
	    	
	    	if($paginator['itemCountPerPage']>0) {
	    		$page = $paginator['currentPage'];
	    		$rowCount = $paginator['itemCountPerPage'];
	    		$select->limitPage($page,$rowCount);
	    	}
	        $result = $db->fetchAll($select);
	    }
	    
	    return $result;
	}
   
   // get info item
   public function getItem($arrParam = null, $options = null) {
   		if(($options['task'] == 'admin-info') || ($options['task'] == 'admin-edit')) {
   			$where  = 'id = ' . $arrParam['id'];
   			$result = $this->fetchRow($where)->toArray();
   		}  
   		return $result; 
   }
   
   // save item
   public function saveItem($arrParam = null, $options = null) {
        if($options['task'] == 'admin-add') {
        	$permission = array('Full Access', 'Limit Access');
        	$permission = $permission[$arrParam['permission']];
   	        $row = $this->fetchNew();
	   		$row->group_name 	= $arrParam['group_name'];
	   		$row->avatar 		= $arrParam['avatar'];
	   		$row->ranking 		= $arrParam['ranking'];
	   		$row->group_acp 	= $arrParam['group_acp'];
	   		$row->group_default = $arrParam['group_default'];
	   		$row->created 		= date("Y-m-d");
	   		$row->created_by 	= 1;
	   		$row->status 		= $arrParam['status'];
	   		$row->order 		= $arrParam['order'];
	   		$row->permission    = $permission;
	   			
	   		$row->save();
    	}
    	
    	if($options['task'] == 'admin-edit') {
    		$permission = array('Full Access', 'Limit Access');
    		$permission = $permission[$arrParam['permission']];
    		
    		$id    = $arrParam['id'];
    		$where = 'id = ' . $id;
    		
    		$row =  $this->fetchRow($where);
    		
    		$row->group_name 	= $arrParam['group_name'];
    		$row->avatar 		= $arrParam['avatar'];
    		$row->ranking 		= $arrParam['ranking'];
    		$row->group_acp 	= $arrParam['group_acp'];
    		$row->group_default = $arrParam['group_default'];
    		$row->modified      = date("Y-m-d");
    		$row->modified_by   = 1;
    		$row->status 		= $arrParam['status'];
    		$row->order 		= $arrParam['order'];
    		$row->permission    = $permission;
    		
    		$row->save();
    	}
   }
   
   // delete item
   public function deleteItem($arrParam = null, $options = null) {
       if($options['task'] == 'admin-delete') {
       	   $where = ' id = ' . $arrParam['id'];
       	   $this->delete($where);
       }
       
       if($options['task'] == 'admin-multi-delete') {
           $cid = $arrParam['cid'];
           if(count($cid) > 0) {
               $ids   = implode(',', $cid);
               $where = 'id IN (' . $ids . ')';
               $this->delete($where);
           }
       }
   }
   
   // change status
   public function changeStatus($arrParam = null, $options = null) {
   	   
   	   // Multi change
   	   $cid = $arrParam['cid'];
   	   if(count($cid) > 0) {
   	        if($arrParam['type'] == 1){
				$status = 1;
			}else{
				$status = 0;
			}
			
			$ids   = implode(',', $cid);
			$where = 'id IN (' . $ids . ')';
			$data  =  array('status'=>$status);
			$this->update($data, $where);
   	   }
   	   
   	   // change only one item
   	   if($arrParam['id'] > 0) {
	        $status = $arrParam['type'];
	        $data   = array('status'=>$status);
	        $where  = 'id = ' . $arrParam['id'];
	        $this->update($data, $where);
   	   }	
   }
   
   // orderItems
   public function sortItems($arrParam = null, $options = null) {
       $order = $arrParam['order'];
       $cid   = $arrParam['cid'];
       if(count($cid) > 0) {
           foreach ($cid as $key => $value) {
           	   $where = 'id = ' . $value;
               $data  = array('order' => $order[$value]);
               $this->update($data, $where);
           }
   
       }
   }
}